package manager

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/manager"
	managerReq "gitcode.com/G-YT/ANN/server/model/manager/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type MessageRemindApi struct {
}

var mrService = service.ServiceGroupApp.ManagerServiceGroup.MessageRemindService

// SaveMessageRemind 創建消息提醒
// @Tags MessageRemind
// @Summary 創建消息提醒
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body manager.MessageRemind true "創建消息提醒"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /mr/createMessageRemind [post]
func (mrApi *MessageRemindApi) SaveMessageRemind(c *gin.Context) {
	var mr manager.MessageRemind
	err := c.ShouldBindJSON(&mr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Remind": {utils.NotEmpty()},
	}
	if err := utils.Verify(mr, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := mrService.SaveMessageRemind(&mr); err != nil {
		global.GVA_LOG.Error("保存失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("保存成功", c)
	}
}

// DeleteMessageRemind 删除消息提醒
// @Tags MessageRemind
// @Summary 删除消息提醒
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body manager.MessageRemind true "删除消息提醒"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /mr/deleteMessageRemind [delete]
func (mrApi *MessageRemindApi) DeleteMessageRemind(c *gin.Context) {
	id := c.Query("ID")
	if err := mrService.DeleteMessageRemind(id); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteMessageRemindByIds 批量删除消息提醒
// @Tags MessageRemind
// @Summary 批量删除消息提醒
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除消息提醒"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /mr/deleteMessageRemindByIds [delete]
func (mrApi *MessageRemindApi) DeleteMessageRemindByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	if err := mrService.DeleteMessageRemindByIds(ids); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateMessageRemind 更新消息提醒
// @Tags MessageRemind
// @Summary 更新消息提醒
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body manager.MessageRemind true "更新消息提醒"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /mr/updateMessageRemind [put]
func (mrApi *MessageRemindApi) UpdateMessageRemind(c *gin.Context) {
	var mr manager.MessageRemind
	err := c.ShouldBindJSON(&mr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := mrService.UpdateMessageRemind(mr); err != nil {
		global.GVA_LOG.Error("打分失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("打分成功", c)
	}
}

// FindMessageRemind 用id査詢消息提醒
// @Tags MessageRemind
// @Summary 用id査詢消息提醒
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query manager.MessageRemind true "用id査詢消息提醒"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /mr/findMessageRemind [get]
func (mrApi *MessageRemindApi) FindMessageRemind(c *gin.Context) {
	userID := c.Query("userID")
	if result, err := mrService.GetMessageRemind(userID); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(result, "獲取成功", c)
	}
}

// GetMessageRemindList 分页獲取消息提醒列表
// @Tags MessageRemind
// @Summary 分页獲取消息提醒列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query managerReq.MessageRemindSearch true "分页獲取消息提醒列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /mr/getMessageRemindList [get]
func (mrApi *MessageRemindApi) GetMessageRemindList(c *gin.Context) {
	var pageInfo managerReq.MessageRemindSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := mrService.GetMessageRemindInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetMessageRemindRecord 獲取消息提醒记录
func (mrApi *MessageRemindApi) GetMessageRemindRecord(c *gin.Context) {
	var mm manager.MessageRemind
	err := c.ShouldBindQuery(&mm)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := c.Query("userID")
	uid, _ := strconv.ParseUint(userID, 10, 64)
	uidUint := uint(uid)
	mm.UserID = uidUint
	if list, err := mrService.GetMessageRemindRecord(mm); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(list, "獲取成功", c)
	}
}
